from dash import Dash, dcc, html
from dash.dependencies import Output, Input
from dash_bootstrap_templates import load_figure_template
import dash_bootstrap_components as dbc
import plotly.express as px
import pandas as pd
# Load and preprocess the dataset
video_games = (
pd.read_csv("vgchartz-2024.csv", parse_dates=["release_date"])
.rename({
"title": "Title",
"console": "Console",
"genre": "Genre",
"publisher": "Publisher",
"developer": "Developer"
}, axis=1)
.assign(release_year=lambda x: x["release_date"].dt.year)
)
dbc_css = "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.min.css"
# Initialize the Dash app
app = Dash(__name__, external_stylesheets=[dbc.themes.CYBORG, dbc_css])
# Load the template for consistent styling
load_figure_template("CYBORG")
# Define the layout of the app
app.layout = dbc.Container([
html.H1("Video Game Explorer", style={"text-align": "center"}),
dbc.Row([
dbc.Col([
dbc.Card([
dcc.Markdown("**Select Category**"),
dcc.Dropdown(
id="category-dropdown",
options=[
{"label": "Title", "value": "Title"},
{"label": "Genre", "value": "Genre"},
{"label": "Publisher", "value": "Publisher"},
{"label": "Developer", "value": "Developer"},
{"label": "Console", "value": "Console"}
],
value="Title",
className="dbc"
),
])
]),
dbc.Col([
dbc.Card([
dcc.Markdown("**Select Region**"),
dcc.RadioItems(
id="region-radio",
options=[
{"label": "World Total", "value": "total_sales"},
{"label": "North America", "value": "na_sales"},
{"label": "Japan", "value": "jp_sales"},
{"label": "Europe/Africa", "value": "pal_sales"},
{"label": "Rest of World", "value": "other_sales"}
],
value="total_sales",
className="dbc"
),
])
]),
]),
html.Br(),
dbc.Row(dcc.Graph(id="sales-line")),
html.Br(),
dbc.Row(dcc.Graph(id="rankings-bar")),
])
# Define the callback to update the graphs
@app.callback(
[Output("sales-line", "figure"),
Output("rankings-bar", "figure")],
[Input("category-dropdown", "value"),
Input("region-radio", "value")]
)
def vg_plotter(category, region):
# Line chart: annual sales
annual_sales = video_games.groupby("release_year", as_index=False).agg({region: "sum"})
fig = px.line(
annual_sales,
x="release_year",
y=region,
title=f"Video Game Sales in {region} Over Time",
line_shape="spline"
).update_traces(line_color="#32CD32")
# Bar chart: top 10 sellers
top10_sellers = (
video_games
.groupby(category, as_index=False)
.agg({region: "sum"})
.sort_values(region, ascending=False)
.iloc[:10]
)
fig2 = px.bar(
top10_sellers,
x=category,
y=region,
color=region,
color_continuous_scale=["#98FB98", "#006400"], # Light green to dark green
title=f"Top Video Sales by {category} in {region}"
)
return fig, fig2
# Run the server
if __name__ == "__main__":
app.run_server(debug=True)